<!--
 * @Date: 2019-12-19 17:05:07
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-04-29 10:42:18
 * @describe: -委派管理-
 -->
<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-house__houseentrust}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.id" placeholder="id" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item> -->
        <el-form-item v-if="HousingID">
          <el-button
            v-if="$hasPermission('house:houseentrust:save')&&hasPermissionInfo('house:houseentrust:save')"
            type="primary"
            @click="addOrUpdateHandle()"
          >写委办</el-button>
        </el-form-item>
        <el-form-item v-else>
          <el-button
            v-if="$hasPermission('house:houseentrust:save')"
            type="primary"
            @click="addOrUpdateHandle()"
          >写委办</el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="HousingID?$hasPermission('house:houseentrust:delete')&&hasPermissionInfo('house:houseentrust:delete'):$hasPermission('house:houseentrust:delete')"
            type="danger"
            @click="deleteHandle()"
          >{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        style="width: 100%;"
      >
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <!-- <el-table-column prop="id" label="主键" header-align="center" align="center"></el-table-column>
        <el-table-column prop="houseId" label="房源id" header-align="center" align="center"></el-table-column>-->
        <el-table-column prop="entrustUserName" label="委托人" header-align="center" align="center"></el-table-column>
        <el-table-column
          prop="entrustDate"
          label="委托日期"
          header-align="center"
          align="center"
          width="100"
        >
          <template
            slot-scope="scope"
          >{{scope.row.entrustDate!=null?scope.row.entrustDate.substring(0,10):scope.row.entrustDate}}</template>
        </el-table-column>
        <el-table-column
          prop="entrustTers"
          label="委托开始期限"
          header-align="center"
          align="center"
          width="100"
        >
          <template
            slot-scope="scope"
          >{{scope.row.entrustTers!=null?scope.row.entrustTers.substring(0,10):scope.row.entrustTers}}</template>
        </el-table-column>
        <el-table-column
          prop="entrustTere"
          label="委托结束期限"
          header-align="center"
          align="center"
          width="100"
        >
          <template
            slot-scope="scope"
          >{{scope.row.entrustTere!=null?scope.row.entrustTere.substring(0,10):scope.row.entrustTere}}</template>
        </el-table-column>
        <!-- <el-table-column prop="number" label="协议编号" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="userId" label="责任人" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="deptId" label="责任部门" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="occupyArea" label="产权面积" header-align="center" align="center"></el-table-column>
        <el-table-column prop="floorArea" label="使用面积" header-align="center" align="center"></el-table-column>-->
        <!-- <el-table-column prop="prices" label="总价" header-align="center" align="center"></el-table-column>
        <el-table-column prop="price" label="单价" header-align="center" align="center"></el-table-column>-->
        <el-table-column prop="floorPrice" label="底价" header-align="center" align="center"></el-table-column>
        <el-table-column prop="deposit" label="保证金" header-align="center" align="center"></el-table-column>
        <el-table-column prop="payType" label="付款方式" header-align="center" align="center"></el-table-column>
        <el-table-column prop="tenanContractTime" label="合同年限" header-align="center" align="center"></el-table-column>
        <el-table-column prop="increase" label="递增" header-align="center" align="center"></el-table-column>
        <el-table-column prop="tenanFreeTime" label="免租时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="servicePrice" label="佣金" header-align="center" align="center"></el-table-column>
        <el-table-column prop="remake" label="备注" header-align="center" align="center"></el-table-column>
        <el-table-column prop="situation" label="房屋状态" header-align="center" align="center"></el-table-column>
        <!-- <el-table-column prop="creator" label="创建者" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="createDate" label="创建时间" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="updater" label="修改人" header-align="center" align="center"></el-table-column> -->
        <!-- <el-table-column prop="updateDate" label="修改时间" header-align="center" align="center"></el-table-column> -->
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              v-if="HousingID?$hasPermission('house:houseentrust:update')&&hasPermissionInfo('house:houseentrust:update'):$hasPermission('house:houseentrust:update')"
              type="text"
              size="small"
              @click="addOrUpdateHandle(scope.row.id)"
            >{{ $t('update') }}</el-button>
            <el-button
              v-if="HousingID?$hasPermission('house:houseentrust:delete')&&hasPermissionInfo('house:houseentrust:delete'):$hasPermission('house:houseentrust:delete')"
              type="text"
              size="small"
              @click="deleteHandle(scope.row.id)"
            >{{ $t('delete') }}</el-button>

            <el-button
              v-if="HousingID?$hasPermission('house:houseentrustimage:upload')&&hasPermissionInfo('house:houseentrustimage:upload'):$hasPermission('house:houseentrustimage:upload')"
              type="text"
              size="small"
              @click="saveImage(scope.row.id)"
            >添加图片</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="ZWbox"></div>
      <div class="paginationBox">
        <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="limit"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle"
        ></el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
        :houseId="HousingID"
      ></add-or-update>
    </div>

    <el-dialog title="上传图片" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="imgUploadFrom" label-width="50px" label-position="top">
        <el-form-item label="备注">
          <el-input v-model="imgUploadFrom.title"></el-input>
        </el-form-item>
        <el-form-item label="选择图片">
          <el-upload
            class="upload-demo"
            ref="FJupload"
            action="/house/houseentrust/upload"
            :file-list="fileListFJ"
            :auto-upload="false"
            :limit="1"
            list-type="picture-card"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="IMGuploadFun" :loading="BtnLoading">点击上传</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./houseentrust-add-or-update";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/house/houseentrust/page",
        getDataListIsPage: true,
        exportURL: "/house/houseentrust/export",
        deleteURL: "/house/houseentrust",
        deleteIsBatch: true
      },
      dataForm: {
        houseId: this.HousingID
      },
      imgUploadFrom: {
        title: "",
        id: ""
      },
      fileListFJ: [],
      BtnLoading: false,
      dialogVisible: false
    };
  },
  props: ["HousingID", "permissionListDTO"],
  components: {
    AddOrUpdate
  },
  created() {
    // if (this.HousingID) {
    //   console.log("有ID");
    // } else {
    //   console.log("没ID");
    // }
  },
  methods: {
    saveImage(id) {
      this.imgUploadFrom.id = id;
      this.imgUploadFrom.title = "";
      this.fileListFJ = [];
      this.dialogVisible = true;
    },
    //上传委办图片
    IMGuploadFun() {
      let _this = this;
      _this.BtnLoading = true;
      let formData = new FormData();
      formData.append("file", this.$refs.FJupload.uploadFiles[0].raw);
      this.$http
        .post("/house/houseentrust/upload", formData, {
          params: {
            entrustId: _this.imgUploadFrom.id,
            title: _this.imgUploadFrom.title
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
            _this.BtnLoading = false;
          }
          this.$message.success("上传成功！");
          this.fileListFJ = [];
          _this.dialogVisible = false;
          _this.BtnLoading = false;
        })
        .catch(() => {});
    },

    //判断操作权限
    hasPermissionInfo(val) {
      let _this = this;
      let arr = _this.permissionListDTO;
      if (arr && arr != []) {
        for (let i = 0; i < arr.length; i++) {
          const element = arr[i];
          if (element.permission.indexOf(val) != -1 && element.state == true) {
            return true;
          }
        }
      } else {
        return false;
      }
    }
  },
  watch: {
    HousingID: {
      handler(newValue, oldValue) {
        this.dataForm.houseId = newValue;
        this.query();
      }
    }
    // $route(to, from) {
    //   if (to.name == "HousingPages" || to.name == "house-page-houseentrust") {
    //       console.log(4321);
    //     //     // console.log("详情页");
    //     //     this.HousingID = this.$route.query.ID;
    //     //     this.transactionType = this.$route.query.transactionType;
    //     //     this.type = this.$route.query.type;
    //     //     this.getHouseAgent();
    //   }
    // }
  }
};
</script>